package com.liangtian.mdm.system.organization.service.impl;

import com.liangtian.mdm.system.organization.dao.SysOrganizationDao;
import com.liangtian.mdm.system.organization.dao.SysOrganizationPermissionDao;
import com.liangtian.mdm.system.organization.model.SysOrganization;
import com.liangtian.mdm.system.organization.service.SysOrganizationService;
import com.liangtian.mdm.system.permission.dao.SysPermissionDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Encoding    :  UTF-8</p>
 * <p>Package Name: com.liangtian.mdm.system.service.sysOrganization.impl </p>
 * <p>Project Name: chccig </p>
 * <p>Copyright   :  Copyright(c) 2016 石化盈科信息技术有限责任公司 </p>
 * <p>Created Time: 2016-04-25 14:18</p>
 * <p>Description :        </p>
 *
 * @author : wangyq
 * @version :  1.0
 */
@Service
public class SysOrganizationServiceImpl implements SysOrganizationService {
    private static final Logger logger = LoggerFactory.getLogger(SysOrganizationServiceImpl.class);

    //注入模块DAO接口对象
    @Autowired
    SysOrganizationDao sysOrganizationDao;

    @Autowired
    SysOrganizationPermissionDao sysOrganizationPermissionDao;
    @Autowired
    SysPermissionDao sysPermissionDao;

    /**
     * @param params
     * @return 组织机构页面对象
     * @desc 根据组织机构父id获取所有的子机构，如果父id为空，查询所有的组织机构
     */
    @Override
    public PageInfo queryAllOrganizationList(Map<String, Object> params) {
        Integer pageNum = (Integer) params.get("pageNum");
        Integer length = (Integer) params.get("length");
        PageHelper.startPage(pageNum, length);
        List<Map<String, Object>> organizationList = sysOrganizationDao.queryAllOrganizationList(params);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(organizationList);
        return pageInfo;
    }

    /**
     * @param params
     * @return 返回符合条件的组织结构列表
     * @desc 根据条件获取全部的组织机构信息
     */
    @Override
    public List<Map<String, Object>> queryOrganizationList(Map<String, Object> params) {
        return sysOrganizationDao.queryAllOrganizationList(params);
    }

    /**
     * @param sysOrganization 对象
     * @return 受影响的行数
     * @des 新增组织机构
     */
    @Override
    public int insertOrganization(SysOrganization sysOrganization) {
        return sysOrganizationDao.insertOrganization(sysOrganization);
    }

    /**
     * @param sysOrganization 对象
     * @param sysOrganization 对象
     * @return 受影响的行数
     * @desc 编辑 （编辑数据；启用-1；禁用-0）
     */
    @Override
    public int updateOrganization(SysOrganization sysOrganization) {
        return sysOrganizationDao.updateOrganization(sysOrganization);
    }

    /**
     * @param sysOrganization 对象
     * @param ids             资源id
     * @return 受影响的行数
     * @desc 编辑机构权限
     */
    @Override
    public int updateOrganizationPermission(SysOrganization sysOrganization, Long[] ids) {
        Map<String, Object> paramsId = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        paramsId.put("organizationId", sysOrganization.getOrganizationId());
        int count = 0;
        //删除前查询中间表是否有记录
        int queryCount = sysOrganizationPermissionDao.queryOrganizationPermission(paramsId);
        if (queryCount > 0) {
            //删除机构关联的资源
            sysOrganizationPermissionDao.deleteOrPerByOrganizationId(paramsId);
        }
        //机构权限中间表插入数据
        for (Long permissionId : ids) {
            if (permissionId > 0) {
                params.put("organizationId", sysOrganization.getOrganizationId());
                params.put("permissionId", permissionId);
                count = sysOrganizationPermissionDao.insertOrganizationPermission(params);
            }
        }
        return count;
    }

    /**
     * @param params ids
     * @return 受影响的行数
     * @desc 批量删除
     */
    @Override
    public int batchDeleteOrganization(Map<String, Object> params) {
        //删除组织机构权限关联信息
        sysOrganizationPermissionDao.batchDeleteOrganizationPermission(params);
        return sysOrganizationDao.batchDeleteOrganization(params);
    }

    /**
     * @param params
     * @return 查询到的树形组织机构
     * @desc 查询树状结构组织机构
     */
    @Override
    public List<Map<String, Object>> queryOrganizationMapListForTree(Map<String, Object> params) {
        return sysOrganizationDao.queryOrganizationMapListForTree(params);
    }

    /**
     * 查询子组织机构
     *
     * @param params
     * @return
     */
    @Override
    public List<SysOrganization> querySubOrganizationMapList(Map<String, Object> params) {
        return sysOrganizationDao.querySubOrganizationMapList(params);
    }

    /**
     * @param params
     * @return 受影响的行数
     * @desc 编辑组织机构状态
     */
    @Override
    public int editOrganizationStatus(Map<String, Object> params) {
        return sysOrganizationDao.editOrganizationStatus(params);
    }

    /**
     * @param params
     * @return 单个机构对象
     * @desc 根据id查询机构对象
     */
    @Override
    public Map<String, Object> queryOrganizationById(Map<String, Object> params) {
        return sysOrganizationDao.queryOrganizationById(params);
    }

    /**
     * @param params
     * @return 相同的机构名称个数
     * @desc 查询是否有相同的机构名称
     */
    @Override
    public int querySameOrganizationNameCounts(Map<String, Object> params) {
        return sysOrganizationDao.querySameOrganizationNameCounts(params);
    }

    /**
     * @param params
     * @return 全部启用状态下的权限树
     * @desc 获取机构关联权限树
     */
    @Override
    public List<Map<String, Object>> queryPermissionMapListForTree(Map<String, Object> params) {
        return sysPermissionDao.queryPermissionMapListForOrTree(params);
    }

    /*** <p>Title:</p>
     * <p>Description: TODO</p>
     * @param : @param orgId
     * @param : @return
     * @author : xiaoliu
     * @date :2016年10月27日下午3:33:39
     */

    @Override
    public List<SysOrganization> queryOrgByName(String orgName) {

        return sysOrganizationDao.queryOrgByName(orgName);
    }
}
